package com.hermit.qmst.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.VideoView;

import com.hermit.qmst.R;
import com.hermit.qmst.UI.activitys.ImageFullScreenActivity;
import com.hermit.qmst.bean.BottleBean;
import com.hermit.qmst.tools.Common;
import com.hermit.qmst.tools.Utils;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2016/11/30 0030.
 */
public class BottleAdapter extends BaseAdapter {
    private String TAG = "BottleAdapter";
    public static List<BottleBean> data;
    private Context mContext;

    public BottleAdapter(Context context) {
        data = new ArrayList<>();
        this.mContext = context;
    }

    public void addData(ArrayList<BottleBean> data) {
        if (data != null) {
            this.data.addAll(data);
        }
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if (view == null) {
            viewHolder = new ViewHolder();
            view = LayoutInflater.from(mContext).inflate(R.layout.list_item_bottle, null);
            x.view().inject(viewHolder, view);
            viewHolder.vv_video.setMediaController(new MediaController(mContext));
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.mTvTitle.setText(data.get(i).getTitle());
        viewHolder.mTvContext.setText(data.get(i).getContext());
        if (data.get(i).getPic() != null && data.get(i).getPic().size() > 0) {
            viewHolder.mGvImg.setVisibility(View.VISIBLE);
            BottleImageAdapter adapter = new BottleImageAdapter(mContext, data.get(i).getPic());
            viewHolder.mGvImg.setAdapter(adapter);
            final BottleBean bottleBeen = data.get(i);
            viewHolder.mGvImg.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    Intent intent = new Intent(mContext, ImageFullScreenActivity.class);
                    // 图片url,为了演示这里使用常量，一般从数据库中或网络中获取
                    if (bottleBeen != null && bottleBeen.getPic().size() > 0) {
                        String urls[] = new String[bottleBeen.getPic().size()];
                        for (int j = 0; j < bottleBeen.getPic().size(); j++) {
                            urls[j] = Common.mCommUrl + bottleBeen.getPic().get(j);
                        }
                        intent.putExtra(ImageFullScreenActivity.EXTRA_IMAGE_URLS, urls);
                        intent.putExtra(ImageFullScreenActivity.EXTRA_IMAGE_INDEX, i);
                        mContext.startActivity(intent);
                    }
                }
            });
        } else {
            viewHolder.mGvImg.setVisibility(View.GONE);
        }
        if (data.get(i).getVideo() != null && !"".equals(data.get(i).getVideo())) {
            viewHolder.vv_video.setVisibility(View.VISIBLE);
            viewHolder.vv_video.setVideoURI(Uri.parse(Common.mCommUrl + data.get(i).getVideo()));
            viewHolder.vv_video.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    VideoView v1 = (VideoView) v;
                    v1.start();
                }
            });
        } else {
            viewHolder.vv_video.setVisibility(View.GONE);
        }
        viewHolder.mTvTime.setText(Utils.stampToDate(data.get(i).getCreateDate().getSec()+""));
        return view;
    }

    static class ViewHolder {
        @ViewInject(R.id.tv_title)
        TextView mTvTitle;
        @ViewInject(R.id.tv_context)
        TextView mTvContext;
        @ViewInject(R.id.gv_img)
        GridView mGvImg;
        @ViewInject(R.id.vv_video)
        VideoView vv_video;
        @ViewInject(R.id.tv_time)
        TextView mTvTime;
    }

}